import { Column, Entity, JoinTable, ManyToMany, Relation } from 'typeorm';
import { CommonEntity } from '~/common/entity/common.entity';
import { UserEntity } from '../../user/user.entity';
import { MenuEntity } from '../menu/menu.entity';

@Entity({ name: 'sys_role' })
export class RoleEntity extends CommonEntity {
  @Column({ length: 50, unique: true })
  name: string;

  @Column({ unique: true, comment: '角色标识' })
  value: string;

  @Column({ nullable: true })
  remark: string;

  @Column({ type: 'tinyint', nullable: true, default: 1 })
  status: number;

  @Column({ nullable: true })
  default: boolean;

  @ManyToMany(() => UserEntity, (user) => user.roles)
  users: Relation<UserEntity[]>;

  @ManyToMany(() => MenuEntity, (menu) => menu.roles, {})
  @JoinTable({
    name: 'sys_role_menus',
    joinColumn: { name: 'role_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'menu_id', referencedColumnName: 'id' }
  })
  menus: Relation<MenuEntity[]>;
}
